<template>
  <div>
    <van-nav-bar
      title="订单页"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-contact-card
      add-text="添加地址"
      type="add"
      @click="onAdd"
      v-if="list.length === 0"
    />
    <div class="address" @click="goAddress" v-else>
      <van-address-list
        v-model="chosenAddressId"
        :list="list"
        :disabled-list="disabledList"
        default-tag-text="默认"
        @edit="onEdit"
      />
    </div>

    <van-empty image="error" description="暂无订单" />
    <van-submit-bar :price="price" button-text="提交订单" @submit="onSubmit" />
  </div>
</template>

<script>
import { defaultAddress, updateAddress } from "../../service/address.js";
export default {
  data() {
    return {
      show: false, // 是否显示新增地址
      price: 0,
      tel: "",
      name: "",
      list: [
        // {
        //   id: "1",
        //   name: "张三",
        //   tel: "13000000000",
        //   address: "浙江省杭州市西湖区文三路 138 号东方通信大厦 7 楼 501 室",
        //   isDefault: true,
        // },
      ], // 存储默认地址
      chosenAddressId: "", // 选择的地址的id
    };
  },
  created() {
    this.getDefaultAddress();
    // async function defaultAddressAsync(){

    // }
    // const res = await defaultAddress({ userid });
    // console.log(res);
  },
  methods: {
    getDefaultAddress() {
      const userid = window.localStorage.getItem("userid");
      // console.log(userid);
      defaultAddress({ userid })
        .then((res) => {
          const resObj = res.data.data[0];
          console.log(resObj);
          const obj = {
            id: resObj.userid,
            name: resObj.name,
            tel: resObj.tel,
            address: resObj.province + resObj.city + resObj.county + resObj.addressDetail,
            isDefault: true,
          };
          (this.tel = obj.tel), (this.name = obj.name);
          this.list.push(obj);
          // console.log(this.list);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    onClickLeft() {
      history.back();
    },
    goAddress() {
      this.$router.push("/addressManagement");
    },
    onAdd() {
      this.$router.push("/addressManagement");
    },
    onEdit(item, index) {
      console.log(item, index);
      this.dataList = item; // 存储要修改或删除的数据
      updateAddress(item).then((res) => {
        if (res.data.code === "200") {
          this.getNewAddressList();
        }
      });
    },
    onSubmit() {
      console.log("提交订单");
    },
  },
};
</script>
<style scoped>
.van-contact-card {
  margin-top: 20px;
}
.van-address-list {
  height: 0px;
}
/* 穿透 */
:deep(.van-address-list__bottom) {
  position: static;
  padding: 0px;
  display: none;
  border-radius: 0px;
}
</style>
